package ipmsg.etc;

import java.text.SimpleDateFormat;
import java.util.Date;













public class UtilityGlobal
{
  public static boolean isIP(String ip)
  {
    String[] content = ip.split("\\.");
    if (content.length != 4) return false;
    try
    {
      for (int i = 0; i < 4; i++) {
        int j = Integer.parseInt(content[i]);
        if ((j > 255) || (j < 0)) return false;
      }
    } catch (NumberFormatException e) {
      return false;
    }
    return true;
  }
  



  public static String getTime()
  {
    Date curTime = new Date();
    String sss = new String("yyyy-MM-dd  kk:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat(sss);
    String res = sdf.format(curTime);
    return res;
  }
  



  public static int getNumber(String number)
  {
    try
    {
      return new Integer(number).intValue();
    }
    catch (NumberFormatException e) {}
    return -1;
  }
  
  public static int atoi(String para)
  {
    if ((para == null) || (para.trim().length() == 0)) {
      return 0;
    }
    char PLUS = '+';
    char MINUS = '-';
    int ret = 0;
    String str = para.trim();
    int len = str.length();
    
    int pos = len;
    
    for (int i = 0; i < len; i++) {
      char ch = str.charAt(i);
      if ((i != 0) || (
        (ch != '+') && (ch != '-')))
      {


        if ((ch < '0') || (ch > '9')) {
          pos = i;
          break;
        } }
    }
    try {
      if (str.charAt(0) == '+') {
        str = str.substring(1);
        pos--;
      }
      ret = Integer.parseInt(str.substring(0, pos));
    }
    catch (Exception e) {}
    return ret;
  }
  




  public static String dec2Hex(int dec)
  {
    StringBuffer sb = new StringBuffer();
    
    for (int i = 0; i < 8; i++) {
      int tmp = dec >> (7 - i % 8) * 4 & 0xF;
      if (tmp < 10) {
        sb.append(tmp);
      } else
        sb.append((char)(65 + (tmp - 10)));
    }
    return sb.toString();
  }
}
